##########################################################
# x varname
# vx varnames to check against for varname beeing unique
##########################################################
cphsoc_varname_remove_illegal_chars <- function(x,vx,ptrn="")
{
  if(nchar(ptrn)==0) return(x)
  if(length(grep(ptrn,x))==0) return(x)
  vtmp <- strsplit(x,ptrn)[[1]]
  if(length(vtmp)==1) # last char, add empty string
  {
    vtmp <- c(vtmp,"")
  }
  xout <- paste(vtmp,collapse='X',sep='')
  
  if(is.na(match(xout,vx))) return(xout)
  
  for(ch in LETTERS)
  {
    xout <- paste(vtmp,collapse=ch,sep='')
    if(is.na(match(xout,vx))) return(xout)
  }
  # not enough try with two letters
  for(ch1 in LETTERS)
  {
    for(ch2 in LETTERS)
    {
      xout <- paste(vtmp,collapse=paste(ch1,ch2,collapse='',sep=''),sep='')
      if(is.na(match(xout,vx))) return(xout)
    }
  }
  # if unsuccessful return NA
  NA
}